<extend name="Layout:layout_content" />
<block name="head_after">
    <style type="text/css">
        .xsui-table-bordered tfoot td { font-weight: bold; color:#F00; font-size:14px; padding:8px 0;}
        #form td a { color:#FFF;}
        #form td { background-color:#09C;}
    </style>
</block>
<block name="body_main">
    <div class="grid-parent xsui-page-header">
        <div class="grid-parent">
            <span style="font-size:16px; font-family: '微软雅黑'; font-weight:bold; margin-right:20px; color:#F90;">[{$store_data['title']}-日报表]</span>
            <span class="xsui-input-group">
                <form method="get">               
                    开始日期：<input type="text" name="date_start" class="datepicker" size="10" placeholder="开始日期" value="{$search.date_start}" />
                    结束日期：<input type="text" name="date_end" class="datepicker" size="10" placeholder="结束日期" value="{$search.date_end}" />
                    旅行社：<select name="travel_agency_id" data-url="{:U('TravelAgencyConf/get_json_list','store_id='.$search['store_id'])}" title="旅行社" data-value="{$search.travel_agency_id}"><option>请选择</option></select>
                    客源类型：<select name="tourist_type_id" data-url="{:U('TouristTypeConf/get_json_list','store_id='.$search['store_id'])}" title="客源类型" data-value="{$search.tourist_type_id}"><option>请选择</option></select>
                    讲解厅：<select name="interpretation_room_id" data-url="{:U('InterpretationRoomConf/get_json_list','store_id='.$search['store_id'])}" title="讲解厅" data-value="{$search.interpretation_room_id}"><option>请选择</option></select>
                    销售类型：<select name="sale_type" title="销售类型" data-value="{$search.sale_type}"><option value="">请选择</option><option value="现场">现场</option><option value="定金">定金</option></select>
                    讲解员：<select name="interpreter_id" data-url="{:U('InterpreterConf/get_json_list','store_id='.$search['store_id'])}" title="讲解员" data-value="{$search.interpreter_id}"><option>请选择</option></select>
                    导游信息：<select name="guide_id" data-url="{:U('GuideConf/get_json_list','store_id='.$search['store_id'])}" title="导游信息" data-value="{$search.guide_id}"><option>请选择</option></select>
                    <input type="text" name="search_key" value="{$search.search_key}" placeholder="车牌/卡号/客源地/销售类型" />
                    <button type="submit" class="ui-state-default"><i class="fa fa-search"></i>搜索</button>
                    <input type="hidden" name="store_id" value="{$search.store_id}" />                 
                </form>
            </span>
        </div>
    </div>
    <div id="grid-container">
        {$content}
    </div>
</block>
<block name="body_after">
    <script type="text/javascript">
        $(function () {
            // 定金类型整行颜色区分
            $('.sale_type_cell:contains("定金")').parent().find('td').css('background-color', '#F0F2AA');
            // 2016-5-26
            ajax_combobox({selector: '.xsui-input-group select[name="travel_agency_id"]', def_option: '选择'});
            ajax_combobox({selector: '#form select[name="travel_agency_id"]', def_option: '选择', func: travel_change});
            ajax_combobox({selector: 'select[name="tourist_type_id"]', def_option: '选择'});
            ajax_combobox({selector: 'select[name="interpretation_room_id"]', def_option: '选择'});
            ajax_combobox({selector: 'select[name="interpreter_id"]', def_option: '选择'});
            ajax_combobox({selector: 'select[name="guide_id"]', def_option: '选择'});
            $('#form select[name="travel_agency_id"]').change(function () {
                travel_change();
            })
            // 编辑表单 2016-5-27
            $('.xsui-lnk-edit').click(function () {
                var $form = $('#form');
                $.get($(this).data('url'), function (data) {
                    for (var attr in data) {
                        $form.find('[name="' + attr + '"]').val(data[attr]);
                    }
                    // 2016-6-3
                    $form.find('select[name="commission_rate_id"]').data('value', data.commission_rate_id);
                    travel_change();
                })
                return false;
            })
            // 提交表单 2016-5-26
            $('#lnk-save').click(function () {
                var $form = $('#form');
                // 表单验证
                var el, err = "", label;
                var skip_validate_fields = [];
                if ('定金' == $form.find('[name="sale_type"]').val()) { // 2016-6-3
                    skip_validate_fields = ['license_plate_number', 'card_number', 'person_count', 'interpretation_room_id', 'interpreter_id', 'guide_id'];
                }else{
                    skip_validate_fields=['deposit_date'];
                }
                $form.find('input,select').each(function () {
                    el = $(this);
                    if (el.prop('required') && $.inArray(el.attr('name'), skip_validate_fields) < 0) {
                        if (!el.val()) {
                            //console.log(el.val());
                            label = el[0].tagName == "INPUT" ? el.prop('placeholder') : el.attr('title');
                            err += (label ? label : el.attr('name')) + "无效!\n";
                        }
                    }
                })
                if (err) {
                    alert(err);
                    return false;
                }
                // 金额验证
                if (!check_amount()) {
                    return false;
                }
                // 提交表单
                var param = $form.find('input, select').serialize();
                $.post($form.data('action'), param, function (result) {
                    //console.log(result);
                    if (result.status) {
                        alert('保存成功!');
                        location.reload();
                    } else {
                        alert(result.info);
                    }
                })
                return false;
            })
            // 2016-6-3
            function travel_change() {
                var val = $('#form select[name="travel_agency_id"]').val();
                if (!val) {
                    return;
                }
                var url = $('#form select[name="commission_rate_id"]').data('url').replace('varid', val);
                console.log(url);
                ajax_combobox({selector: '#form select[name="commission_rate_id"]', url: url, def_option: '选择'});
            }
            // 金额验证 2016-5-27
            /*
             sichou_amount,sijin_amount,baihuo_amount
             cash_amount,credit_amount
             field_amount,deposit_amount
             product_amount
             */
            function check_amount() {
                var amounts = {
                    sichou_amount: parseInt($('input[name="sichou_amount"]').val()),
                    sijin_amount: parseInt($('input[name="sijin_amount"]').val()),
                    baihuo_amount: parseInt($('input[name="baihuo_amount"]').val()),
                    cash_amount: parseInt($('input[name="cash_amount"]').val()),
                    credit_amount: parseInt($('input[name="credit_amount"]').val()),
                    product_amount: parseInt($('input[name="product_amount"]').val()),
                };
                // 检测空值
                /* 2016-6-3
                 if (amounts.product_amount < 1) {
                 alert('总额不能为0!');
                 return false;
                 }
                 var label, err = "";
                 for (var attr in amounts) {
                 if ((attr != 'cash_amount' && attr != 'credit_amount') && !amounts[attr]) {
                 console.log(amounts[attr]);
                 label = $('input[name="' + attr + '"]').prop('placeholder');
                 err += label + "不能为空!\n";
                 }
                 }
                 if (err) {
                 alert(err);
                 return false;
                 }*/
                var v1 = amounts.sichou_amount + amounts.sijin_amount + amounts.baihuo_amount;
                var v2 = amounts.cash_amount + amounts.credit_amount;
                if (v1 != amounts.product_amount) {
                    alert('(丝绸销售+丝巾销售+百货销售)与(总额)不一致!');
                    return false;
                }
                if (v2 != amounts.product_amount) {
                    alert('(现金金额+刷卡金额)与(总额)不一致!');
                    return false;
                }
                return true;
            }
            // 自动计算 2016-5-27
            $('input[name="sichou_amount"],input[name="sijin_amount"],input[name="baihuo_amount"]').blur(function () {
                calc_amount();
            })
            // 自动计算 2016-5-27
            function calc_amount() {
                var amounts = {
                    sichou_amount: parseInt($('input[name="sichou_amount"]').val()),
                    sijin_amount: parseInt($('input[name="sijin_amount"]').val()),
                    baihuo_amount: parseInt($('input[name="baihuo_amount"]').val()),
                };
                /* 2016-6-3
                 var err = false;
                 for (var attr in amounts) {
                 if (!amounts[attr]) {
                 err = true;
                 break;
                 }
                 }
                 if (err) {
                 return;
                 }*/
                $('input[name="product_amount"]').val((amounts.sichou_amount + amounts.sijin_amount + amounts.baihuo_amount));
            }
        })
    </script>
</block>